package com.newlink.building.common_base.widget

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import com.newlink.building.common_base.R
import com.newlink.building.common_base.databinding.DialogCustomBinding

/**
 * @author wangyang
 * @date 2022/10/11
 * @description 自定义弹窗（信息、确定、取消）
 */
class CustomDialog(context: Context) : Dialog(context, R.style.toast_setting_dialog) {

    private var mMessage: String = ""
    private var mNegative: String = ""
    private var mPositive: String = ""
    private var negativeListener: OnClickListener? = null
    private var positiveListener: OnClickListener? = null
    private lateinit var mBinding: DialogCustomBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = DialogCustomBinding.inflate(LayoutInflater.from(context))
        setContentView(mBinding.root)
        mBinding.tvTitleCustomDialog.text = mMessage
        mBinding.btnCancel.text = mNegative
        mBinding.btnConfirm.text = mPositive

        mBinding.btnCancel.setOnClickListener {
            negativeListener?.onClick(this)
        }
        mBinding.btnConfirm.setOnClickListener {
            positiveListener?.onClick(this)
        }
    }

    fun setMessage(message: String): CustomDialog {
        mMessage = message
        return this
    }

    fun setNegativeButton(
        listener: OnClickListener
    ): CustomDialog {
        this.mNegative = context.resources.getString(R.string.cancel_dialog_custom)
        this.negativeListener = listener
        return this
    }

    fun setNegativeButton(
        text: String = context.resources.getString(R.string.cancel_dialog_custom),
        listener: OnClickListener
    ): CustomDialog {
        this.mNegative = text
        this.negativeListener = listener
        return this
    }

    fun setPositiveButton(
        listener: OnClickListener
    ): CustomDialog {
        this.mPositive = context.resources.getString(R.string.confirm_dialog_custom)
        this.positiveListener = listener
        return this
    }

    fun setPositiveButton(
        text: String = context.resources.getString(R.string.confirm_dialog_custom),
        listener: OnClickListener
    ): CustomDialog {
        this.mPositive = text
        this.positiveListener = listener
        return this
    }

    interface OnClickListener {
        fun onClick(dialog: CustomDialog)
    }
}